<template>
  <div class="p-2">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div class="search" v-show="showSearch">
        <el-form :model="queryParams" ref="queryFormRef" :inline="true" label-width="68px">
          <el-form-item label="模板名称" prop="templateName">
            <el-input v-model="queryParams.templateName" placeholder="请输入" clearable style="width: 240px" @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAdd" v-hasPermi="['credit:billTemplate:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" icon="Edit" :disabled="single" @click="handleUpdate" v-hasPermi="['credit:billTemplate:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" :disabled="multiple" @click="handleDelete" v-hasPermi="['credit:billTemplate:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" icon="Download" @click="handleExport" v-hasPermi="['credit:billTemplate:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="billTemplateList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="模板名称" align="center" prop="templateName" width="150"/>
        <el-table-column label="拆分金额范围" align="center" prop="minCardMoney" width="200">
          <template #default="scope">
            {{scope.row.minCardMoney}} ~ {{scope.row.maxCardMoney}}
          </template>
        </el-table-column>
        <el-table-column label="POS出账占比" align="center" prop="posOutTotal" width="120">
          <template #default="scope">
            {{scope.row.posOutTotal}} %
          </template>
        </el-table-column>
        <el-table-column label="扫码出账占比" align="center" prop="qrOutTotal" width="120">
          <template #default="scope">
            {{scope.row.qrOutTotal}} %
          </template>
        </el-table-column>
        <el-table-column label="入账次数" align="center" prop="inCount">
          <template #default="scope">
            {{scope.row.inCount}} 次
          </template>
        </el-table-column>
        <el-table-column label="POS刷卡次数" align="center" prop="posOutCount" width="120">
          <template #default="scope">
            {{scope.row.posOutCount}} 次
          </template>
        </el-table-column>
        <el-table-column label="扫码出账次数" align="center" prop="qrOutCount" width="120">
          <template #default="scope">
            {{scope.row.qrOutCount}} 次
          </template>
        </el-table-column>
        <el-table-column label="POS单笔出账范围" align="center" prop="posOutSingleMin" width="160">
          <template #default="scope">
            {{scope.row.posOutSingleMin}}% ~ {{scope.row.posOutSingleMax}}%
          </template>
        </el-table-column>
        <el-table-column label="扫码单笔出账范围" align="center" prop="qrOutSingleMin" width="160">
          <template #default="scope">
            {{scope.row.qrOutSingleMin}}% ~ {{scope.row.qrOutSingleMax}}%
          </template>
        </el-table-column>
        <el-table-column label="进出时间间隔" align="center" prop="outIntervalMinute" width="150">
          <template #default="scope">
            <div v-if="scope.row.outIntervalMinute == 60">1小时</div>
            <div v-else-if="scope.row.outIntervalMinute == 120">2小时</div>
            <div v-else-if="scope.row.outIntervalMinute == 180">3小时</div>
            <div v-else-if="scope.row.outIntervalMinute == 240">4小时</div>
            <div v-else>｛｛scope.row.outIntervalMinute｝｝分钟</div>
          </template>
        </el-table-column>
        <el-table-column label="模板状态" align="center" prop="state">
          <template #default="scope">
            <dict-tag :options="module_state" :value="scope.row.state" />
          </template>
        </el-table-column>
        <el-table-column label="排序索引" align="center" prop="sort" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['credit:billTemplate:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['credit:billTemplate:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>
    <!-- 添加或修改账单拆分模板对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="900px" append-to-body>
      <el-form ref="billTemplateFormRef" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="模板名称" prop="templateName">
              <el-input v-model="form.templateName" placeholder="请输入模板名称" style="width: 40%"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="适配金额" prop="minCardMoney" style="display: flex;">
              <span style="margin: 0 10px;">【 </span>
              <el-input v-model="form.minCardMoney" placeholder="最小金额" style="width: 30%">
              </el-input>
              <span style="margin: 0 10px;"> ~ </span>
              <el-input v-model="form.maxCardMoney" placeholder="最大金额" style="width: 30%">
              </el-input>
              <span style="margin: 0 10px;">】</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="模板状态" prop="state">
              <el-radio-group v-model="form.state">
                <el-radio v-for="dict in module_state"
                          :key="parseInt(dict.value)"
                          :label="parseInt(dict.value)">
                  {{dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入账范围" prop="inSingleMin">
              <span style="margin: 0 10px;">【 </span>
              <el-input v-model="form.inSingleMin" placeholder="入账最低" style="width:70px;"></el-input>
              <span style="margin: 0 10px;">% ~ </span>
              <el-input v-model="form.inSingleMax" placeholder="入账最高" style="width:70px;"></el-input>
              <span style="margin: 0 10px;">% 】 </span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="入账次数" prop="inCount">
              <el-input v-model="form.inCount" placeholder="请输入pos入账次数">
                <template #append>次</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="POS出账占比" prop="posOutTotal">
              <el-input v-model="form.posOutTotal" placeholder="请输入POS出账占比">
                <template #append>%</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="扫码出账占比" prop="qrOutTotal">
              <el-input v-model="form.qrOutTotal" placeholder="请输入扫码出账占比">
                <template #append>%</template>
              </el-input>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="POS执行利率" prop="posRate">-->
<!--              <el-input v-model="form.posRate" placeholder="请输入pos执行利率">-->
<!--                <template #append>%</template>-->
<!--              </el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item label="扫码执行利率" prop="qrRate">-->
<!--              <el-input v-model="form.qrRate" placeholder="请输入扫码执行利率">-->
<!--                <template #append>%</template>-->
<!--              </el-input>-->
<!--            </el-form-item>-->
<!--          </el-col>-->

          <el-col :span="12">
            <el-form-item label="POS出账次数" prop="posOutCount">
              <el-input v-model="form.posOutCount" placeholder="请输入pos出账次数">
                <template #append>次</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="扫码出账次数" prop="qrOutCount">
              <el-input v-model="form.qrOutCount" placeholder="请输入扫码出账次数">
                <template #append>次</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="POS出账范围" prop="posOutSingleMin">
              <span style="margin: 0 10px;">【 </span>
              <el-input v-model="form.posOutSingleMin" placeholder="最低金额" style="width: 70px;">
              </el-input>

              <span style="margin: 0 10px;">% ~ </span>
              <el-input v-model="form.posOutSingleMax" placeholder="最大金额" style="width: 70px;" >
              </el-input>

              <span style="margin: 0 10px;">%】 </span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="扫码出账范围" prop="qrOutSingleMin">
              <span style="margin: 0 10px;">【 </span>
              <el-input v-model="form.qrOutSingleMin" placeholder="最小金额" style="width: 70px;">
<!--                <template #append>元/笔</template>-->
              </el-input>
              <span style="margin: 0 10px;">% ~ </span>
              <el-input v-model="form.qrOutSingleMax" placeholder="最大金额" style="width: 70px;">
<!--                <template #append>元/笔</template>-->
              </el-input>
              <span style="margin: 0 10px;">%】 </span>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="进出账间隔" prop="outIntervalMinute">
              <el-select v-model="form.outIntervalMinute" placeholder="请选择进出账间隔" clearable style="width:100%;">
                <el-option :key="60" label="1小时" :value="60" />
                <el-option :key="120" label="2小时" :value="120" />
                <el-option :key="180" label="3小时" :value="180" />
                <el-option :key="240" label="4小时" :value="240" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="排序索引" prop="sort">
              <el-input v-model="form.sort" placeholder="请输入排序索引" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BillTemplate" lang="ts">
import { listBillTemplate, getBillTemplate, delBillTemplate, addBillTemplate, updateBillTemplate } from '@/api/credit/bill/template';
import { BillTemplateVO, BillTemplateQuery, BillTemplateForm } from '@/api/credit/bill/template/types';

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { module_state } = toRefs<any>(proxy?.useDict('module_state'));

const billTemplateList = ref<BillTemplateVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();
const billTemplateFormRef = ref<ElFormInstance>();

const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

const initFormData: BillTemplateForm = {
  id: undefined,
  templateName: undefined,
  // posRate: undefined,
  // qrRate: undefined,
  minCardMoney: undefined,
  maxCardMoney: undefined,
  posOutCount: undefined,
  qrOutCount: undefined,
  inCount: undefined,
  inSingleMin: undefined,
  inSingleMax: undefined,
  posOutTotal: undefined,
  posOutSingleMin: undefined,
  posOutSingleMax: undefined,
  qrOutTotal: undefined,
  qrOutSingleMin: undefined,
  qrOutSingleMax: undefined,
  outIntervalMinute:undefined,
  state: undefined,
  sort: undefined,
}
const data = reactive<PageData<BillTemplateForm, BillTemplateQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    templateName: undefined,
    params: {
    }
  },
  rules: {
    templateName: [
      { required: true, message: "模板名称不能为空", trigger: "blur" }
    ],
    // posRate: [
    //   { required: true, message: "pos机执行利率不能为空", trigger: "blur" }
    // ],
    // qrRate: [
    //   { required: true, message: "扫码执行利率不能为空", trigger: "blur" }
    // ],
    minCardMoney: [
      { required: true, message: "模板适配最小金额不能为空", trigger: "blur" }
    ],
    maxCardMoney: [
      { required: true, message: "模板适配最大金额不能为空", trigger: "blur" }
    ],
    posOutCount: [
      { required: true, message: "pos机刷卡次数不能为空", trigger: "blur" }
    ],
    qrOutCount: [
      { required: true, message: "扫码不能为空", trigger: "blur" }
    ],
    inCount: [
      { required: true, message: "入账次数不能为空", trigger: "blur" }
    ],
    posOutTotal: [
      { required: true, message: "pos出账占比不能为空", trigger: "blur" }
    ],
    posOutSingleMin: [
      { required: true, message: "pos机每笔最小金额不能为空", trigger: "blur" }
    ],
    posOutSingleMax: [
      { required: true, message: "pos机每笔最大金额不能为空", trigger: "blur" }
    ],
    qrOutTotal: [
      { required: true, message: "扫码出账占比不能为空", trigger: "blur" }
    ],
    qrOutSingleMin: [
      { required: true, message: "扫码不能为空", trigger: "blur" }
    ],
    qrOutSingleMax: [
      { required: true, message: "扫码不能为空", trigger: "blur" }
    ],
    inSingleMin: [
      { required: true, message: "进账每笔最小金额不能为空", trigger: "blur" }
    ],
    inSingleMax: [
      { required: true, message: "进账每笔最大金额不能为空", trigger: "blur" }
    ],
    outIntervalMinute: [
      { required: true, message: "请选择进出账时间间隔", trigger: "blur" }
    ],
    state: [
      { required: true, message: "模板状态 0 - 不可用 1 - 可用不能为空", trigger: "blur" }
    ],
    sort: [
      { required: true, message: "排序索引不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询账单拆分模板列表 */
const getList = async () => {
  loading.value = true;
  const res = await listBillTemplate(queryParams.value);
  billTemplateList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  billTemplateFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: BillTemplateVO[]) => {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  dialog.visible = true;
  dialog.title = "添加账单拆分模板";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: BillTemplateVO) => {
  reset();
  const _id = row?.id || ids.value[0]
  const res = await getBillTemplate(_id);
  Object.assign(form.value, res.data);
  dialog.visible = true;
  dialog.title = "修改账单拆分模板";
}

/** 提交按钮 */
const submitForm = () => {
  billTemplateFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      if (form.value.id) {
        await updateBillTemplate(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addBillTemplate(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("修改成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: BillTemplateVO) => {
  const _ids = row?.id || ids.value;
  await proxy?.$modal.confirm('是否确认删除账单拆分模板编号为"' + _ids + '"的数据项？').finally(() => loading.value = false);
  await delBillTemplate(_ids);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('system/billTemplate/export', {
    ...queryParams.value
  }, `billTemplate_${new Date().getTime()}.xlsx`)
}

onMounted(() => {
  getList();
});
</script>
